﻿@using System.Globalization;
<Card Margin="Margin.Is4.FromBottom" Border="Border.Rounded" Class="pricing-card" Height="Height.Is100" TextAlignment="TextAlignment.Start">
    <CardBody Flex="Flex.Column.AlignItems.Start" Gap="Gap.Is3" Padding="Padding.Is0.FromBottom">
        <Heading Size="HeadingSize.Is4" Margin="Margin.Is0" TextWeight="TextWeight.Normal" TextColor="@TextAccentColor">
            @PlanName
        </Heading>
        <Div Width="Width.Is100">
            <Div Height="Height.Px( 50 )" Flex="Flex.AlignItems.Baseline" Gap="Gap.Is3">
                @if ( ShowHigherPlan )
                {
                    <Span TextSize="TextSize.Heading3" TextColor="TextColor.Muted" TextDecoration="@(ShowDiscount ? TextDecoration.LineThrough : TextDecoration.Default)">@TotalHigherPlanPriceString</Span>
                    @if ( ShowDiscount )
                    {
                        <Span TextSize="TextSize.Heading4" TextColor="TextColor.Success" TextWeight="TextWeight.SemiBold">
                            <Icon Name="IconName.ArrowDown" />
                            @PercentageDiscountString
                        </Span>
                    }
                }
            </Div>
            <Div Flex="Flex.JustifyContent.Start.AlignItems.Center" Gap="Gap.Is1">
                @if ( ShowPlan )
                {
                    <Span TextSize="TextSize.Heading1" Flex="Flex.InlineFlex">
                        <Span>@CurrencySymbol</Span>
                        <Span>@TotalPlanPriceString</Span>
                    </Span>

                    @if ( PlanPrice > 0 )
                    {
                        <Span Flex="Flex.InlineFlex.Column">
                            <Span TextSize="TextSize.ExtraSmall" TextColor="TextColor.Muted" TextWeight="TextWeight.Light">@CurrencyCode</Span>
                            <Span TextSize="TextSize.Small" TextColor="TextColor.Muted" TextWeight="TextWeight.Light">/@PlanUnit</Span>
                        </Span>
                    }
                }
            </Div>
        </Div>
        @if ( ButtonContent is not null )
        {
            @ButtonContent
        }
        <Div Width="Width.Is100">
            @if ( IncludesContent is not null )
            {
                <Div TextSize="TextSize.Default" TextColor="@TextAccentColor">
                    @IncludesContent
                </Div>
                <Divider />
            }
            @ChildContent
        </Div>
    </CardBody>
</Card>

@code {
    string TotalPlanPriceString => PlanPrice switch
    {
        0 => "Free",
        -1 => "Custom",
        _ => ( PlanPrice * Quantity ).ToString( CultureInfo.InvariantCulture )
    };

    string TotalHigherPlanPriceString
        => ShowHigherPlan ? ( HigherPlanPrice * Quantity ).ToString( CultureInfo.InvariantCulture ) : null;

    decimal HigherPlanPrice => ( PlanPrice * 12 ) / 10;

    string PercentageDiscountString => $"{( 100 - ( ( PlanPrice / HigherPlanPrice ) * 100 ) ).ToString( "F0", CultureInfo.InvariantCulture )}%";

    [Parameter] public string PlanName { get; set; }

    [Parameter] public bool ShowPlan { get; set; }

    [Parameter] public bool ShowHigherPlan { get; set; }

    [Parameter] public bool ShowDiscount { get; set; }

    [Parameter] public decimal Quantity { get; set; }

    [Parameter] public decimal PlanPrice { get; set; }

    [Parameter] public string PlanUnit { get; set; }

    [Parameter] public string CurrencySymbol { get; set; }

    [Parameter] public string CurrencyCode { get; set; }

    [Parameter] public TextColor TextAccentColor { get; set; }

    [Parameter] public RenderFragment ButtonContent { get; set; }

    [Parameter] public RenderFragment IncludesContent { get; set; }

    [Parameter] public RenderFragment ChildContent { get; set; }
}
